package yunhe.biz.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import yunhe.biz.ISaleBiz;
import yunhe.dao.ISaleDao;
import yunhe.entity.Sale;
import yunhe.entity.SaleAppend;
import yunhe.entity.SalePack;
import yunhe.util.SqlSessionFactoryUtil;

public class SaleBizImpl implements ISaleBiz {

	/**
	 * 查询所有销售单
	 */
	public List<Sale> saleList() {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleList();
		return list;
	}

	
	/**
	 * 查询所有未执行销售单
	 */
	public List<Sale> saleListUnExcute() {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleListUnExcute();
		return list;
	}
	/**
	 * 根据销售单号改变为执行状态
	 */
	public int alterStateBySalesId(String salesId) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		int flag=empDao.alterStateBySalesId(salesId);
		session.commit();
		return flag;
	}
	/**
	 * 根据销售单编号查询销售单
	 */
	public List<Sale> saleListBySalesId(String salesId) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleListBySalesId(salesId);
		return list;
	}
	/**
	 * 根据销售单编号查询销售单及从表
	 */
	public List<SaleAppend> saleListAndAppendAndProBySalesId(String salesId) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<SaleAppend>  list = empDao.saleListAndAppendAndProBySalesId(salesId);
		return list;
	}
	/**
	 * 根据顾客查询销售单及从表信息
	 */
	public List<SaleAppend> saleListAndAppendAndProByCusId(String cusId){
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<SaleAppend>  list = empDao.saleListAndAppendAndProByCusId(cusId);
		return list;
	}

	/**
	 * 根据顾客id查询销售单
	 */
	public List<Sale> saleListByCustomerId(String customerId) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleListByCustomerId(customerId);
		return list;
	}

	/**
	 * 根据销售日期查询销售单
	 */
	public List<Sale> saleListBySaleDate(Date date1, Date date2) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		List<Sale> list = empDao.saleListBySaleDate(date1, date2);

		return list;
	}

	/**
	 * 根据经办人id查询销售单
	 */
	public List<Sale> saleListByWorkId(String workId) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleListByWorkId(workId);
		return list;
	}

	/**
	 * 根据应收金额查询销售单
	 */
	public List<Sale> saleListByAccountReceiving(double money) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleListByAccountReceiving(money);
		return list;
	}

	/**
	 * 根据实收金额查询销售单
	 */
	public List<Sale> saleListByAccountReceived(double money) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleListByAccountReceived(money);
		return list;
	}

	/**
	 * 根据执行状态查询销售单
	 */
	public List<Sale> saleListByExcuteState(int state) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleListByExcuteState(state);
		return list;
	}

	/**
	 * 销售退货历史
	 */
	public List<SaleAppend> saleBackHistory() {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<SaleAppend> list = empDao.saleBackHistory();
		return list;
	}
	/**
	 * 根据日期查询退货历史
	 */
	public  List<SaleAppend> saleBackHistoryByDate(Date date1,Date date2){
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		List<SaleAppend> list = empDao.saleBackHistoryByDate(date1, date2);

		return list;
	}
	/**
	 * 销售排行
	 */
	public List<Sale> saleRank() {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<Sale> list = empDao.saleRank();
		return list;
	}

	/**
	 * 销售合同生成
	 */
	public int salePackGenerate(SalePack salePack) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		int flag=empDao.salePackGenerate(salePack);
		session.commit();
		return flag;
	}

	/**
	 * 销售合同查询
	 */
	@Override
	public List<SalePack> salePackQuery() {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);

		List<SalePack> list = empDao.salePackQuery();
		return list;
	}
	/**
	 * 根据日期查询销售合同
	 */
	public List<SalePack> salePackQueryByDate(Date date1,Date date2){
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		List<SalePack> list = empDao.salePackQueryByDate(date1, date2);

		return list;
	}
	/**
	 * 销售录入
	 * 
	 * @param sale
	 * @return
	 */
	public int saleIn(Sale sale) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		int flag=empDao.saleIn(sale);
		session.commit();
		return flag;
	}
	/**
	 * 销售从表录入
	 * 
	 * @param saleAppend
	 * @return
	 */
	public int saleAppendIn(SaleAppend saleAppend) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		int flag=empDao.saleAppendIn(saleAppend);
		session.commit();
		return flag;
	}

	/**
	 * 根据客户进行退货1
	 * 
	 * @param cusId
	 * @return
	 */
	public int saleBackQuantityAlterByCus(String cusId) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		int flag=empDao.saleBackQuantityAlterByCus(cusId);
		session.commit();
		return flag;
	}
	/**
	 * 根据客户进行退货2
	 *
	 * @param customerId
	 * @return
	 */
	public int saleBackStateAlterByCus(String customerId) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();

		ISaleDao empDao = (ISaleDao) session.getMapper(ISaleDao.class);
		int flag=empDao.saleBackStateAlterByCus(customerId);
		session.commit();
		return flag;
	}









}
